# -*- coding: utf-8 -*-

from common.mymako import render_mako_context
from django.http import HttpResponse,JsonResponse
from blueking.component.shortcuts import get_client_by_request
from blueking.component.shortcuts import get_client_by_user
from .extends.paramikotool import *
from models import *
def index(request):
    client = get_client_by_request(request)
    # 组件参数
    kwargs = {'app_id': 1}
    result = client.cc.get_app_host_list(kwargs)
    return HttpResponse('Hello World huangsheng')

def home(request):
    """
    首页
    """
    return render_mako_context(request, '/batch/home.html')


def dev_guide(request):
    """
    开发指引
    """
    return render_mako_context(request, '/batch/dev_guide.html')


def contactus(request):
    """
    联系我们
    """
    configlist = [{'ip':'10.83.14.171','username':'root','password':'123123','sshport':'22','cmd':'ps -ef |grep GciTaskA_SUB_A00|grep -v grep','mark':'GciTaskA_SUB_A00'},
        {'ip': '10.83.14.171', 'username': 'root', 'password': '123123', 'sshport': '22','cmd': 'ps -ef |grep GciTaskC_SUB_C02|grep -v grep','mark': 'GciTaskC_SUB_C02'},
        {'ip': '10.83.14.166', 'username': 'root', 'password': '123123', 'sshport': '22','cmd': 'ps -ef |grep GciTaskE_SUB_E03|grep -v grep','mark': 'GciTaskE_SUB_E03'},
                  {'ip': '10.83.14.166', 'username': 'root', 'password': '123123', 'sshport': '22',
                   'cmd': 'ps -ef |grep GciTaskF_SUB_F04|grep -v grep','mark': 'GciTaskF_SUB_F04'},
                  {'ip': '10.83.14.166', 'username': 'root', 'password': '123123', 'sshport': '22',
                   'cmd': 'ps -ef |grep GciTaskG_SUB_G05|grep -v grep','mark': 'GciTaskG_SUB_G05'}
                  ]
    for item in configlist:
        sshconfig.objects.create(ip=item['ip'],username=item['username'],password=item['password'],sshport=item['sshport'],
                                 cmd=item['cmd'],mark=item['mark'])
    print 'ok'
    return render_mako_context(request, '/batch/contact.html')

def process(request):
    pt = ParamikoTool()
    configlist=sshconfig.objects.all().filter(ip='10.83.14.171').values()
    # configlist = [{'ip':'10.83.14.171','username':'root','password':'123123','sshport':'22','cmd':'ps -ef |grep GciTaskA_SUB_A00|grep -v grep','mark':'GciTaskA_SUB_A00'},
    #     {'ip': '10.83.14.171', 'username': 'root', 'password': '123123', 'sshport': '22','cmd': 'ps -ef |grep GciTaskC_SUB_C02|grep -v grep','mark': 'GciTaskC_SUB_C02'},
    #     {'ip': '10.83.14.166', 'username': 'root', 'password': '123123', 'sshport': '22','cmd': 'ps -ef |grep GciTaskE_SUB_E03|grep -v grep','mark': 'GciTaskE_SUB_E03'},
    #               {'ip': '10.83.14.166', 'username': 'root', 'password': '123123', 'sshport': '22',
    #                'cmd': 'ps -ef |grep GciTaskF_SUB_F04|grep -v grep','mark': 'GciTaskF_SUB_F04'},
    #               {'ip': '10.83.14.166', 'username': 'root', 'password': '123123', 'sshport': '22',
    #                'cmd': 'ps -ef |grep GciTaskG_SUB_G05|grep -v grep','mark': 'GciTaskG_SUB_G05'}
    #               ]
    retlist=[]
    for item in configlist:
        try:
            strout = pt.excute(item['ip'],item['username'],int(item['sshport']),item['password'],item['cmd'])
            print strout
            templist=[]
            tempdic = {}
            if strout != '':

                for str in strout.split(' '):
                    if str!='':
                        templist.append(str)
                    print templist
                tempdic['ip'] = item['ip']
                tempdic['user'] = item['username']
                tempdic['pid'] = templist[1]
                tempdic['mark'] = item['mark']
                tempdic['statu'] = u'运行中'
            else:
                tempdic['ip'] = item['ip']
                tempdic['user'] = item['username']
                tempdic['pid'] = u'未运行'
                tempdic['mark'] = item['mark']
                tempdic['statu'] = u'未运行'
            retlist.append(tempdic)
        except:
            retlist.append({'ip':item['ip'],'user':item['username'],'pid':'','mark':'','statu':'监控机不可达'})
    return render_mako_context(request,'/batch/process.html',{'dic':retlist})
configdic = sshconfig.objects.all().values()

def stopprocess(request):
    print 'start'
    pt = ParamikoTool()

    mark = request.GET.get('mark')
    ip = request.GET.get('ip')
    print ip
    templist = []
    tempdic = {}
    username=''
    sshport=22
    password=''
    for config in configdic:
        if config['ip'] == ip and config['mark'] == mark:
            password = config['password']
            sshport = config['sshport']
            username = config['username']
    cmd = 'ps -ef |grep {0}|grep -v grep|xargs kill -9'.format(mark)
    result = 'true'
    if username!='':
        print cmd
        strout = pt.excute(ip,username, int(sshport),password, cmd)
        print strout
        if strout == 'false':
            result = 'false'
        else:
            result = 'true'
    print result
    return JsonResponse(result, safe=False)

def startprocess(request):
    print 'start'
    pt = ParamikoTool()

    mark = request.GET.get('mark')
    ip = request.GET.get('ip')
    print ip
    templist = []
    tempdic = {}
    username=''
    sshport=22
    password=''
    for config in configdic:
        if config['ip'] == ip and config['mark'] == mark:
            password = config['password']
            sshport = config['sshport']
            username = config['username']
    cmd = '/bqhexin/SubGciTask/{0}/runTask.sh >> /bqhexin/SubGciTask/{0}/log/als_gci.log'.format(mark[:-8])

    print cmd
    result = 'true'
    if username!='':
        strout = pt.excutenoresponse(ip,username, int(sshport),password, cmd)
        print 'strout',strout

        if strout == 'false':
            result = 'false'
        else:
            result = 'true'

    print result
    return JsonResponse(result, safe=False)
